package com.syj.jsxjj.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.syj.jsxjj.R;


public class ViewPagerIndicator extends RelativeLayout {
    private ViewPager pager;
    private int dotGap;
    private int dotSize;
    private int screenWidth;
    private ImageView indicatorImage;
    private int pageCount;

    public ViewPagerIndicator(Context context) {
        super(context);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs,int defStyleAttr){
        super(context, attrs, defStyleAttr);
    }


    public void setViewPager(ViewPager pager, int pageCount) {
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        screenWidth = wm.getDefaultDisplay().getWidth();
        dotGap = screenWidth * 1 / 100;
        dotSize = dotGap;
        this.pager = pager;
        this.pageCount = pageCount;
        for (int i = 0; i < pageCount; i++) {
            ImageView imageView = new ImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            layoutParams.width = dotSize;
            layoutParams.height = dotSize;
            layoutParams.leftMargin = i * (dotGap + dotSize);
            imageView.setLayoutParams(layoutParams);
            imageView.setImageResource(R.mipmap.lunbodian1);
            addView(imageView);
        }
        indicatorImage = new ImageView(getContext());
        indicatorImage.setScaleType(ImageView.ScaleType.FIT_XY);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        layoutParams.leftMargin = 0;
        layoutParams.width = dotSize;
        layoutParams.height = dotSize;
        indicatorImage.setLayoutParams(layoutParams);
        indicatorImage.setImageResource(R.mipmap.lunbodian2);
        addView(indicatorImage);

        if (pageCount < 2) {
            setVisibility(View.INVISIBLE);
        } else {
            setVisibility(View.VISIBLE);
        }
    }


    public void setOnPageScroll(final ViewPager.OnPageChangeListener listener) {
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
                listener.onPageScrolled(i, v, i2);
                if (pager.getCurrentItem() == i) {
                    moveIndicator(pager.getCurrentItem(), v);
                } else {
                    moveIndicator(pager.getCurrentItem(), v - 1f);
                }
            }

            @Override
            public void onPageSelected(int i) {
                listener.onPageSelected(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
                listener.onPageScrollStateChanged(i);
            }
        });
    }

    private synchronized void moveIndicator(int curIndex, float xOffset) {
        if (pageCount < 2) {
            return;
        }
        curIndex = curIndex % pageCount;
        int changeSize = dotSize / 2;
        curIndex = Math.abs(curIndex % pageCount);
        int start = curIndex * (dotSize + dotGap);
        int leftOffset = start + (int) ((float) (dotGap + dotSize) * xOffset);
        if (leftOffset > (pageCount - 1) * (dotSize + dotGap) || leftOffset < 0) {
            return;
        }
        RelativeLayout.LayoutParams layoutParams = (LayoutParams) indicatorImage.getLayoutParams();
        layoutParams.leftMargin = leftOffset;
        /*layoutParams.width = dotSize + (int) ((1 - (Math.abs((Math.abs(xOffset) - 0.5)) / 0.5)) * changeSize);
        layoutParams.height = layoutParams.width;*/
        indicatorImage.setLayoutParams(layoutParams);
    }
}
